package com.taptech.doufu.presenter.novel

import android.app.Activity
import com.taptech.doufu.bean.HomeTopBean
import com.taptech.doufu.bean.ProductionBean
import com.taptech.doufu.bean.ShareBeansInfo
import com.taptech.doufu.net.retrofit.api.BaseSubscriber
import com.taptech.doufu.model.novel.NovelDetailsModel
import com.taptech.doufu.model.novel.bean.*
import com.taptech.doufu.presenter.BasePresenter
import com.taptech.doufu.services.personalcenter.AccountService

class NovelReadEndPresenter(context: Activity, view: INovelReadEndView, val novelId: Int) : BasePresenter<INovelReadEndView>(context, view) {
    private val model = NovelDetailsModel(mContext)
    private var recommendByAuthorBegin = 0
    private var authorId = 0
    private var recommendByAuthorList = mutableListOf<NovelRecommendBean>()
    private var recommendByAuthorIndex = 0
    private var recommendByAuthorHasMore = true
    private var collectTimes = 0    //小说收藏数
    var hasCollect = false //是否收藏了该小说
    var novelBean = NovelDetailBean()


    fun getNovelDetails() {
        if (novelId != null) {
            model.getNovelDetail(novelId,object : BaseSubscriber<NovelDetailBean>(){
                override fun onNext(t: NovelDetailBean?) {
                    super.onNext(t)
                    if (t != null) {
                        novelBean = t
                        authorId = if (t.user.userId.isNullOrEmpty()) 0 else t.user.userId.toInt()
                        hasCollect = t.has_fav
                        collectTimes = if (t.collect_times.isNullOrEmpty()) 0 else t.collect_times.toInt()
                        getRecommendByAuthor()
                        getRecommendByNovel()
                        mView.fillNovelDetail(t)
                    }else{

                    }
                }
            })
        }
    }



    fun getRecommendByAuthor(){
        if (recommendByAuthorHasMore && recommendByAuthorIndex >= recommendByAuthorList.size-6) {
            model.getRecommendNovel(authorId,0, NovelDetailsPresenter.SIZE,recommendByAuthorBegin,object : BaseSubscriber<NovelRecommendGson>(){
                override fun onNext(t: NovelRecommendGson?) {
                    super.onNext(t)
                    if (t != null) {
                        recommendByAuthorBegin = t.begin
                        if (t.list.size < NovelDetailsPresenter.SIZE) {
                            recommendByAuthorHasMore = false
                        }
                        recommendByAuthorList.addAll(t.list)
                    }
                    setCurrRecommendByAuthorList()
                }

                override fun onError(e: Throwable?) {
                    super.onError(e)
                    setCurrRecommendByAuthorList()
                }
            })
        }else{
            setCurrRecommendByAuthorList()
        }
    }


    fun getRecommendByNovel() {
        if (novelId != null) {
            model.getRecommendNovel(0,novelId, 0,0,object : BaseSubscriber<NovelRecommendGson>(){
                override fun onNext(t: NovelRecommendGson?) {
                    super.onNext(t)
                    if (t != null) {
                        mView.fillRecommendByNovel(t.list)
                    }
                }

            })
        }
    }


    private fun setCurrRecommendByAuthorList() {
        val curList = mutableListOf<NovelRecommendBean>()
        if (recommendByAuthorList.size <= 4) {
            when {
                recommendByAuthorList.size == 1 -> curList.add(recommendByAuthorList[0])
                recommendByAuthorList.size == 2 -> {
                    curList.add(recommendByAuthorList[0])
                    curList.add(recommendByAuthorList[1])
                }
                recommendByAuthorList.size == 3 -> {
                    curList.add(recommendByAuthorList[0])
                    curList.add(recommendByAuthorList[1])
                    curList.add(recommendByAuthorList[2])
                }
                recommendByAuthorList.size == 4 ->{
                    curList.add(recommendByAuthorList[0])
                    curList.add(recommendByAuthorList[1])
                    curList.add(recommendByAuthorList[2])
                    curList.add(recommendByAuthorList[3])
                }
            }
            mView.fillRecommendByAuthor(curList,false)
        }else{
            if (recommendByAuthorIndex < recommendByAuthorList.size - 1) {
                recommendByAuthorIndex += 1
            }else{
                recommendByAuthorIndex = 0
            }
            curList.add(recommendByAuthorList[recommendByAuthorIndex])
            if (recommendByAuthorIndex < recommendByAuthorList.size - 1) {
                recommendByAuthorIndex += 1
            }else{
                recommendByAuthorIndex = 0
            }
            curList.add(recommendByAuthorList[recommendByAuthorIndex])
            if (recommendByAuthorIndex < recommendByAuthorList.size - 1) {
                recommendByAuthorIndex += 1
            }else{
                recommendByAuthorIndex = 0
            }
            curList.add(recommendByAuthorList[recommendByAuthorIndex])
            if (recommendByAuthorIndex < recommendByAuthorList.size - 1) {
                recommendByAuthorIndex += 1
            }else{
                recommendByAuthorIndex = 0
            }
            curList.add(recommendByAuthorList[recommendByAuthorIndex])
            mView.fillRecommendByAuthor(curList,true)
        }
    }

    fun praiseComment(id:Int,position:Int,cancel:Boolean/*是否取消赞*/){
        if (cancel) {
            model.cancelPraiseComment(id,object : BaseSubscriber<Boolean>(){
                override fun onNext(t: Boolean?) {
                    super.onNext(t)
                    if (t != null && t) {
                        mView.praiseComment(position,cancel,success = true)
                    }else{
                        mView.praiseComment(position,cancel,success = false)
                    }
                }

                override fun onError(e: Throwable?) {
                    super.onError(e)
                    mView.praiseComment(position,cancel,success = true)
                }
            })
        }else{
            model.praiseComment(id,object : BaseSubscriber<Boolean>(){
                override fun onNext(t: Boolean?) {
                    super.onNext(t)
                    if (t != null && t) {
                        mView.praiseComment(position,cancel,success = true)
                    }else{
                        mView.praiseComment(position,cancel,success = false)
                    }
                }

                override fun onError(e: Throwable?) {
                    super.onError(e)
                    mView.praiseComment(position,cancel,success = true)
                }

            })
        }
    }


    fun collectNovel(){
        val uid  = AccountService.getInstance().userUid
        if (hasCollect){  //取消收藏
            if (novelId != null) {
                model.cancelCollectNovel(uid.toInt(),novelBean.object_type,novelId,object : BaseSubscriber<Boolean>(){
                    override fun onNext(t: Boolean?) {
                        super.onNext(t)
                        if (t != null && t) {
                            collectTimes -=1
                            hasCollect = false
                            mView.collectNovel(false,collectTimes)
                        }else{
                            mView.collectNovel(true,collectTimes)
                        }
                    }

                    override fun onError(e: Throwable?) {
                        super.onError(e)
                        mView.collectNovel(true,collectTimes)
                    }
                })
            }
        }else{  //收藏
            if (novelId != null) {
                model.collectNovel(uid.toInt(),novelBean.object_type,novelId,object : BaseSubscriber<Boolean>(){
                    override fun onNext(t: Boolean?) {
                        super.onNext(t)
                        if (t != null && t) {
                            collectTimes += 1
                            hasCollect = true
                            mView.collectNovel(true,collectTimes)
                        }else{
                            mView.collectNovel(false,collectTimes)
                        }
                    }

                    override fun onError(e: Throwable?) {
                        super.onError(e)
                        mView.collectNovel(false,collectTimes)
                    }
                })
            }
        }
    }


    fun getProductionBean(): ProductionBean {
        val bean = HomeTopBean()
        bean.id = novelId.toString()
        bean.object_type = novelBean.object_type.toString()
        bean.user = novelBean.user
        return bean
    }

    fun getShareBean():ShareBeansInfo{
        val shareBean = ShareBeansInfo()
        shareBean.id = "$novelId"
        shareBean.dataType = 1
        shareBean.shartText = novelBean.des
        shareBean.imagUrl = novelBean.with_image_w
        shareBean.title = novelBean.title
        shareBean.shareUrl = novelBean.shareUrl
        shareBean.object_type = "${novelBean.object_type}"
        return shareBean
    }

}